/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.states;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import towerz.component.panel.HutPanel;
import towerz.system.BitmapFont;
import towerz.system.StateManager;
import towerz.system.StringManager;
import towerz.system.Utilities;

/**
 *
 * @author TimotiusNC
 */
public class StateSplash extends GameState {
	public static final int	MAX_TIMER = 2000;

	public StateSplash(){
		super(GameState.SPLASH);

		//Initialize
		m_timer		= 0;
		m_Drawn		= false;
		m_Loaded	= false;
		
		//Create image
		m_LoadingText = BitmapFont.getFont(BitmapFont.TITLE).renderTransparentText("Now loading...", 0x000000);
		m_PresentText = BitmapFont.getFont(BitmapFont.TITLE).renderTransparentText(StringManager.instance().getString("present"), 0x000000);
		StateManager.instance().loadImages();
	}

	public void update(long time, int keys) {
		//Super
		super.update(time, keys);

		//If button is pressed
		if (m_timer >= (MAX_TIMER + 500)) { 
			//Goto GAME state
			StateManager.instance().goTo(GameState.FRONT, null, true);
		}

		m_timer += time;
		m_progressBar += time;

		if(m_progressBar > MAX_TIMER)
			m_progressBar = MAX_TIMER;
		
		//If drawn and not loaded
		if (m_Drawn && !m_Loaded) {
			//Load stuff
			HutPanel.loadText();
			StateManager.instance().loadSFX();
			
			//Is loaded
			m_Loaded = true;
		}
	}

	public void draw(Graphics g) {
		//Super
		super.draw(g);

		//Draw progres bar
        g.setColor(0xFFFFFFFF);
        g.fillRect(30, 220, (int)((float)m_progressBar / (float)MAX_TIMER * (float)(260)), 8);
		
		//Draw images
		g.drawImage(m_LoadingText, 32, 218, Graphics.LEFT | Graphics.BOTTOM);
		g.drawImage(m_PresentText, StateManager.instance().getWidth() / 2, StateManager.instance().getHeight() / 2, Graphics.HCENTER | Graphics.TOP);
		g.drawImage(StateManager.instance().getDeveloperImage(), StateManager.instance().getWidth() / 2, StateManager.instance().getHeight() / 2, Graphics.HCENTER | Graphics.BOTTOM);
		
		//Is drawn
		m_Drawn = true;
	}

	//ATTRIBUTES
	protected int		m_timer, m_progressBar;
	protected boolean	m_Drawn;
	protected boolean	m_Loaded;
	
	//Images
	protected Image m_LoadingText;
	protected Image m_PresentText;
}
